import Vue from 'vue'
import Vuex from 'vuex'

//导入localStorageHelper工具
import {addGoodsObj,getGoodsObj,getTotalCount, updateGoodsObj,deleteGoodsObj} from '../common/localStorageHelper.js'

Vue.use(Vuex)

const store = new Vuex.Store({
    state:{
        buyCount:0
    },
    getters:{
        //获取商品总数
        getGoodsTotalCount(state){
            if(state.buyCount > 0){
                return state.buyCount
            }else{
                return getTotalCount()
            }
        }
    },
    mutations:{
        //添加商品
        addGoods(state,goods){
            state.buyCount = addGoodsObj(goods)
        },
        //更新商品
        updateGoods(state,goods){
            state.buyCount = updateGoodsObj(goods)
        },
        //删除商品
        deleteGoodsById(state,goodsId){
            state.buyCount = deleteGoodsObj(goodsId)
        }
    }
})

export default store